package com.san.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzk.config.RequireLogin;
import com.hzk.pojo.UserBasic;
import com.hzk.utils.JsonUtil;
import com.hzk.vo.CaseUser;
import com.san.mapper.RuleMapper;
import com.san.message.MQSender;
import com.san.message.applicationMessage;
import com.san.pojo.Rule;
import com.san.pojo.SeckillApplicationForm;
import com.san.service.IUserBasicService;
import com.san.serviceImpl.DroolServiceImpl;
import com.san.serviceImpl.SeckillApplicationFormServiceImpl;
import com.san.vo.RespBean;
import com.san.vo.RespBeanEnum;
import com.san.vo.appVo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Controller
@RequestMapping("/App")
public class application_controller implements InitializingBean {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    DroolServiceImpl droolService;
    @Autowired
    MQSender mqSender;
    @Autowired
    RuleMapper ruleMapper;
    @Autowired
    IUserBasicService iUserBasicService;
    @Autowired
    SeckillApplicationFormServiceImpl seckillApplicationFormService;


    /* @RequestMapping("/fristApp")
     public RespBean application( Long id,Long goodsid){
         UserBasic user = iUserBasicService.getById(id);
         if (user==null){
             return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
         }
         ValueOperations valueOperations = redisTemplate.opsForValue();
        *//* String token = request.getHeader("Authorization");

        UserBasic user = (UserBasic) valueOperations.get("UserBasic:" + token);
        if (user == null) {//用户登陆失效的情况下抛出异常
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }*//*
        Long id1 = user.getId();
        if (!redisTemplate.hasKey("application:" + id1)){//用户没有初筛记录的情况下进入初筛
            droolService.executeRule(user,goodsid);
            applicationMessage applicationMessage = new applicationMessage();
            applicationMessage.setId(id);
            applicationMessage.setUserBasic(user);
            mqSender.ApplicationMessage(JsonUtil.object2JsonStr(applicationMessage));
            return RespBean.success(0);//进入初筛轮询初筛结果
        }
        if ((boolean)valueOperations.get("application:" + id1)){//用户有初筛记录的情况下对
            // 直接返回缓存里的数据不需要再次进入初筛系统
            return RespBean.success(1);
        }else {
            return RespBean.error(RespBeanEnum.Reappllication_ERROR);

        }

    }*/
    @RequireLogin(seconds = 5, maxCount = 5, needLogin = true)
    @RequestMapping(value = "/fristApp")
    public RespBean application(@CaseUser UserBasic userBasic, Long goodsid, HttpServletRequest request) {
        if (userBasic == null) {
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        ValueOperations valueOperations = redisTemplate.opsForValue();

        String s = request.getHeader("Authorization");
        String token = s.substring(4);

        if (!redisTemplate.hasKey("application:" + userBasic.getId())) {//用户没有初筛记录的情况下进入初筛
            applicationMessage applicationMessage = new applicationMessage();
            applicationMessage.setId(goodsid);
            applicationMessage.setUserBasic(userBasic);
            applicationMessage.setToken(token);
            applicationMessage.setRequest(token);
            mqSender.ApplicationMessage(JsonUtil.object2JsonStr(applicationMessage));
            return RespBean.success(0);//进入初筛轮询初筛结果
        }
        if (valueOperations.get("application:" + userBasic.getId()).equals("成功")) {//用户有初筛记录的情况下对
            // 直接返回缓存里的数据不需要再次进入初筛系统
            String getpath = iUserBasicService.getpath(userBasic, goodsid,s);
            return appVo.success(1, token, getpath);

        } else {
            return RespBean.error(RespBeanEnum.Reappllication_ERROR);

        }

    }


    @RequestMapping("/getresult")
    public RespBean getResult(@CaseUser UserBasic user, HttpServletRequest request, Long goodsid) {
        String t = request.getHeader("Authorization");
        if (user == null) {
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        String s = request.getHeader("Authorization");
        String token = s.substring(4);

        ValueOperations valueOperations = redisTemplate.opsForValue();
       /* if (valueOperations.get("application:" + user.getId()).equals("成功")) {
            //用户有初筛记录的情况下对
            return appVo.success(1,token);
        }*/
        // 直接返回缓存里的数据不需要再次进入初筛系统

        //做一个
        SeckillApplicationForm byId = seckillApplicationFormService.getById(user.getId());
        if (byId == null) {
            return RespBean.success(0);//进入轮询
        }
        if (byId.getPass().equals("成功")) {
            String getpath = iUserBasicService.getpath(user, goodsid, t);
            /*return RespBean.success(1);*/

            return appVo.success(1, token, getpath);

        }
        return RespBean.error(RespBeanEnum.Reappllication_ERROR);

    }

    @RequestMapping(value = "/getpath", method = RequestMethod.GET)
    @ResponseBody
    public com.hzk.vo.RespBean getpath(@CaseUser UserBasic user, Long goodsId, HttpServletRequest request) {
        String s = request.getHeader("Authorization");
        if (user == null) {
            return com.hzk.vo.RespBean.error(com.hzk.vo.RespBeanEnum.NULL_USER_ERROR);
        }
        String path = iUserBasicService.getpath(user, goodsId, s);
        if (path == null) {
            return com.hzk.vo.RespBean.error(com.hzk.vo.RespBeanEnum.OPRATION_ERROR);
        }
        return com.hzk.vo.RespBean.success(path);

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        List<Rule> rules = ruleMapper.selectList(new QueryWrapper<Rule>());
        if (CollectionUtils.isEmpty(rules)) {
            return;
        }
        rules.forEach(rule -> {
            redisTemplate.opsForValue().set("rule:" + rule.getId(), rule);
        });
    }
}

